package org.budo.weixin.weixinthirdplatform.api;

import java.io.Serializable;
import java.util.Map;

import org.budo.support.http.client.Request;
import org.budo.support.http.client.Response;
import org.budo.support.lang.util.MapUtil;
import org.budo.support.lang.util.StringUtil;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

import com.alibaba.fastjson.JSON;

/**
 * @author ZRP
 * @version 1 (2016年9月22日下午10:28:38)
 * @since Java7
 */

public class WeixinThirdPlatformApi implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 8922001241912481952L;

    private static final Logger log = Slf4j.getLogger();

    private String componentAppid;

    private String componentAppsecret;

    private String componentVerifyTicket;

    private String componentAccessToken;

    private Long accessTokenCreateTime;

    public String getComponentAppid() {
        return componentAppid;
    }

    public void setComponentAppid(String componentAppid) {
        this.componentAppid = componentAppid;
    }

    public String getComponentAppsecret() {
        return componentAppsecret;
    }

    public void setComponentAppsecret(String componentAppsecret) {
        this.componentAppsecret = componentAppsecret;
    }

    public String getComponentVerifyTicket() {
        return componentVerifyTicket;
    }

    public void setComponentVerifyTicket(String componentVerifyTicket) {
        this.componentVerifyTicket = componentVerifyTicket;
    }

    public void setComponentAccessToken(String componentAccessToken) {
        this.componentAccessToken = componentAccessToken;
    }

    public String getComponentAccessToken() {
        return componentAccessToken;
    }

    public Long getAccessTokenCreateTime() {
        return accessTokenCreateTime;
    }

    public void setAccessTokenCreateTime(Long accessTokenCreateTime) {
        this.accessTokenCreateTime = accessTokenCreateTime;
    }

    public synchronized String findComponentAccessToken() {
        Request request = new Request();
        request.setUrl("https://api.weixin.qq.com/cgi-bin/component/api_component_token");
        request.setMethod(Request.POST);
        Map data = MapUtil.newMap("component_appid", this.getComponentAppid(), //
                "component_appsecret", this.getComponentAppsecret(), //
                "component_verify_ticket", this.getComponentVerifyTicket());
        request.setRequestBody(JSON.toJSONBytes(data));

        Response response = request.execute();
        String result = response.getBody();
        log.info("#82 findComponentAccessToken result=" + result + ", componentAppid=" + this.getComponentAppid());

        Map<String, Object> map = JSON.parseObject(result);
        String token = StringUtil.toString(map.get("component_access_token"));
        if (!StringUtil.isEmpty(token)) {
            return this.componentAccessToken = token;
        }

        log.error("#92 获取微信Token失败, appId={}, appSecret={}, ticket={},response={}", this.componentAppid, this.componentAppsecret, this.componentVerifyTicket, response);
        return this.componentAccessToken;
    }

    public synchronized String findPreAuthCode() {
        Request request = new Request();
        request.setUrl("https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=" + this.getComponentAccessToken());
        request.setMethod(Request.POST);
        Map data = MapUtil.newMap("component_appid", getComponentAppid());
        request.setRequestBody(JSON.toJSONBytes(data));
        Response response = request.execute();

        String result = response.getBody();
        log.info("findPreAuthCode result={}", result);

        Map<String, Object> map = JSON.parseObject(result);
        String preAuthCode = StringUtil.toString(map.get("pre_auth_code"));

        if (StringUtil.isEmpty(preAuthCode)) {
            log.error("获取微信pre_auth_code失败, component_access_token={},response={}", getComponentAccessToken(), response);
        }
        return preAuthCode;
    }

    public Map apiGetAuthorizerInfo(String authorizerAppid) {
        Request request = new Request();
        request.setUrl("https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token=" + getComponentAccessToken());
        request.setMethod(Request.POST);
        Map data = MapUtil.newMap("component_appid", getComponentAppid(), "authorizer_appid", authorizerAppid);
        request.setRequestBody(JSON.toJSONBytes(data));
        Response response = request.execute();
        String result = response.getBody();
        log.info("apiGetAuthorizerInfo result={}", result);
        Map<String, Object> map = JSON.parseObject(result);
        if (null == map.get("authorizer_info")) {
            log.error("获取微信apiGetAuthorizerInfo失败, component_access_token={},response={}", getComponentAccessToken(), response);
        }
        return map;
    }

}
